package com.wframe.project.jnd.util;

import com.frame.database.JdataBean;
import com.wframe.project.jnd.entity.Promotion;
import com.wframe.project.jnd.entity.PurchaseGiftItem;
import com.wframe.project.jnd.entity.PurchaseGiftProduct;
import com.wframe.project.jnd.entity.PurchaseGiftPromotion;
import com.wframe.project.jnd.vo.CartBean;
import com.wframe.project.jnd.vo.CartItem;
import com.wframe.project.jnd.vo.CartProductVO;
import com.wframe.project.jnd.vo.Product;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lwb on 2017/6/5.
 */
@Component
public class PurchaseGiftPromotionBuilder extends ComplexPromotionsBuilder {

    @Override
    public Promotion build(String promotionId) {
        return promotionService.findPurchaseGift(promotionId);
    }

    public CartBean getCartProducts(String userid, String promotionId, List<CartItem> prices, int platform, Map<String, Integer> tempProductStock) throws Exception {
        CartBean bean = new CartBean();
        Map<String, CartItem> map = new HashMap<>();
        int totalAmount = 0;
        for (CartItem item : prices) {
            totalAmount += item.getCount();
            map.put(String.valueOf(item.getProductid()), item);
        }

        //处理有其他促销的情况
        Map<String, Object> process = otherPromotionProcess(userid, map, platform, tempProductStock);
        List<CartProductVO> products = (List<CartProductVO>) process.get("products");
        BigDecimal totalPrice = (BigDecimal) process.get("totalPrice");

        PurchaseGiftPromotion promotion = (PurchaseGiftPromotion) promotionService.findPurchaseGift(promotionId);
        PurchaseGiftPromotionHandlerChain chain = new PurchaseGiftPromotionHandlerChain(promotion.getItems());
        PurchaseGiftItem item = null;
        if (promotion.isBuySend()) {
            //数量满赠
            item = chain.handleAmount(totalAmount);
        } else {
            //金额满赠
            item = chain.handlePrice(totalPrice);
        }
        bean.setStandardMoney(item.getMinMoney());
        bean.setStandardAmount(item.getMinNumber());
        List<PurchaseGiftProduct> gifts = item.getGifts();
        int num = 0;
        for (int i = 0; i < gifts.size(); i++) {
            num += gifts.get(i).getUnit();
        }
        bean.setNumber(num);
        if (item.isAvailable()) {
            bean.setHasStandard(true);

            for (PurchaseGiftProduct product : item.getGifts()) {
                Product base = productService.findOne(product.getProductid());
                CartProductVO productVO = new CartProductVO();
                productVO.setPhoto(base.getPhoto());
                productVO.setName(base.getName());
                productVO.setB_chanjia(base.getManufacturers());
                productVO.setB_guige(base.getSpecification());
                productVO.setGift(true);
                productVO.setId(Integer.parseInt(product.getProductid()));
                productVO.setGiftType(CartProductVO.GiftType.PURCHASE_GIFT);
                productVO.setPrice(BigDecimal.ZERO);
                //赠品数量
                Integer unit = product.getUnit();
                String giftId = product.getProductid();
                //赠品可用库存
                Integer availableInventory = tempProductStock.get(giftId);
                if (availableInventory == null) {
                    availableInventory = productService.getAvailableInventory(giftId);
                }
                //赠品库存是否短缺
                productVO.setShortage(unit > availableInventory);
                unit = unit > availableInventory ? (availableInventory > 0 ? availableInventory : 0) : (unit > 0 ? unit : 0);
                tempProductStock.put(giftId, availableInventory - unit);
                tempProductStock.put(giftId + "_" + promotionId, unit);
                //实际赠品数量
                productVO.setBuyNum(unit);
                productVO.setGiftPromotionId(promotionId);

                products.add(productVO);
            }
        }

        bean.setTotalPrice(totalPrice);
        bean.setProducts(products);
        bean.setPromotionId(promotionId);
        return bean;
    }

  /*  @Override
    public CartBean getData(String userid, String promotionId, List<CartItem> prices, int platform, Map<String, Integer> tempGiftStock) throws Exception {
        CartBean bean = new CartBean();
        List<CartProductVO> products = new ArrayList<>();

        BigDecimal totalPrice = BigDecimal.ZERO;
        int totalAmount = 0;
        for (CartItem item : prices) {
            //处理有其他促销的情况
            CartProductVO productVO = otherPromotionProcess(userid, item, promotionId, platform, tempGiftStock);
            //处理买送促销的赠品
            products = addProduct(products, promotionId, productVO);

            if (item.isCheck()) {
                //满赠促销的总价格
                totalPrice = totalPrice.add(productVO.getTotalPrice());
                //满赠促销的总价格
                totalAmount += productVO.getBuyNum();
            }

            List<String> promotionIds = productVO.getPromotionIds();
            promotionIds.add(promotionId);
            productVO.setPromotionIds(promotionIds);
        }

        PurchaseGiftPromotion promotion = (PurchaseGiftPromotion) promotionService.findPurchaseGift(promotionId);
        PurchaseGiftPromotionHandlerChain chain = new PurchaseGiftPromotionHandlerChain(promotion.getItems());
        PurchaseGiftItem item = null;
        if (promotion.isBuySend()) {
            //数量满赠
            item = chain.handleAmount(totalAmount);
        } else {
            //金额满赠
            item = chain.handlePrice(totalPrice);
        }
        bean.setStandardMoney(item.getMinMoney());
        bean.setStandardAmount(item.getMinNumber());
        List<PurchaseGiftProduct> gifts = item.getGifts();
        int num = 0;
        for (int i = 0; i < gifts.size(); i++) {
            num += gifts.get(i).getUnit();
        }
        bean.setNumber(num);
        if (item.isAvailable()) {
            bean.setHasStandard(true);
            for (CartProductVO product : products) {
                Map promotionAmount = product.getPromotionAmount();
                promotionAmount.put(promotionId, 1);
                product.setPromotionAmount(promotionAmount);
            }

            for (PurchaseGiftProduct product : item.getGifts()) {
                JdataBean info = productService.getProductInfo(product.getProductid());
                CartProductVO productVO = TransformUtils.trans(info, CartProductVO.class);
                productVO.setGift(true);
                productVO.setGiftType(CartProductVO.GiftType.PURCHASE_GIFT);
                productVO.setPrice(BigDecimal.ZERO);
                //赠品数量
                Integer unit = product.getUnit();
                String giftId = product.getProductid();
                //赠品可用库存
                Integer availableInventory = tempGiftStock.get(giftId);
                if (availableInventory == null) {
                    availableInventory = productService.getAvailableInventory(giftId);
                }
                if (availableInventory <= 0) {
                    continue;
                }
                //赠品库存是否短缺
                productVO.setShortage(unit > availableInventory);
                unit = unit > availableInventory ? (availableInventory > 0 ? availableInventory : 0) : (unit > 0 ? unit : 0);
                tempGiftStock.put(giftId, availableInventory - unit);
                tempGiftStock.put(giftId + "_" + promotionId, unit);
                //实际赠品数量
                productVO.setBuyNum(unit);
                productVO.setPromotionId(promotionId);

                products.add(productVO);
            }
        }

        bean.setTotalPrice(totalPrice);
        bean.setProducts(products);
        bean.setPromotionId(promotionId);
        return bean;
    }*/

    public CartBean process(Map.Entry<String, List<JdataBean>> entry, Map<String, Integer> tempProductStock) throws CloneNotSupportedException {
        CartBean result = new CartBean();
        String promotionId = entry.getKey();
        List<CartProductVO> products = new ArrayList<>();

        BigDecimal totalPrice = BigDecimal.ZERO;
        int totalAmount = 0;
        for (JdataBean item : entry.getValue()) {
            //处理有其他促销的情况
            CartProductVO productVO = otherPromotionProcess(item, tempProductStock);
            //处理买送促销的赠品
            products.addAll(addProduct(productVO));

            //满赠促销的总价格
            totalPrice = totalPrice.add(productVO.getTotalPrice());
            //满赠促销的总价格
            totalAmount += productVO.getBuyNum();

            List<String> promotionIds = productVO.getPromotionIds();
            promotionIds.add(promotionId);
            productVO.setPromotionIds(promotionIds);
        }

        PurchaseGiftPromotion promotion = (PurchaseGiftPromotion) promotionService.findPurchaseGift(promotionId);
        PurchaseGiftPromotionHandlerChain chain = new PurchaseGiftPromotionHandlerChain(promotion.getItems());
        PurchaseGiftItem item = null;
        if (promotion.isBuySend()) {
            //数量满赠
            item = chain.handleAmount(totalAmount);
        } else {
            //金额满赠
            item = chain.handlePrice(totalPrice);
        }
        result.setStandardMoney(item.getMinMoney());
        result.setStandardAmount(item.getMinNumber());
        List<PurchaseGiftProduct> gifts = item.getGifts();
        if (item.isAvailable()) {
            for (CartProductVO product : products) {
                Map promotionAmount = product.getPromotionAmount();
                promotionAmount.put(promotionId, 1);
                product.setPromotionAmount(promotionAmount);
            }

            for (PurchaseGiftProduct product : item.getGifts()) {
                CartProductVO productVO = new CartProductVO();
                String giftId = product.getProductid();
                productVO.setId(Integer.parseInt(giftId));
                productVO.setName(product.getName());
                productVO.setGift(true);
                productVO.setGiftType(CartProductVO.GiftType.PURCHASE_GIFT);
                productVO.setPrice(BigDecimal.ZERO);
                //赠品数量
                Integer unit = product.getUnit();
                //赠品可用库存
                Integer availableInventory = tempProductStock.get(giftId);
                if (availableInventory == null) {
                    availableInventory = productService.getAvailableInventory(giftId);
                }
                if (availableInventory <= 0) {
                    continue;
                }
                //赠品库存是否短缺
                productVO.setShortage(unit > availableInventory);
                unit = unit > availableInventory ? (availableInventory > 0 ? availableInventory : 0) : (unit > 0 ? unit : 0);
                tempProductStock.put(giftId, availableInventory - unit);
                tempProductStock.put(giftId + "_" + promotionId, unit);
                //实际赠品数量
                productVO.setBuyNum(unit);
                productVO.setGiftPromotionId(promotionId);

                products.add(productVO);
            }
        }

        result.setTotalPrice(totalPrice);
        result.setProducts(products);
        result.setPromotionId(promotionId);
        return result;
    }

}
